﻿/* CommonComponent for MultiLine add .Net Framework 
 * 程序名称：多行选择主窗体DEMO示例隐藏代码类
 * 程序功能：实现在子窗体中选择多行本窗体中，可在提交前进行编辑、修改、删除等操作。
 * 使用要点：
 * 一、窗体UI呈现
 * 1、复制*.aspx中的<html></html>间的代码
 * 2、修改绑定表字段，详见使用说明。
 * 二、窗体逻辑事件
 * 1、改变有"//maybe to edit"注释的代码
 * 2、详细规则见使用说明
*/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CommonComponent.Common;
using DAL;

public partial class pages_Demo_rowEdit_MultiLineManager : System.Web.UI.Page
{
    #region 私有静态变量
    //  主表数据字段
    const string _TABLE_FIELD_PARENT_PKID = "UID";     //maybe(主表的PKID，在视图中也应有该字段)
    const string _TABLE_FIELD_PARENT_SID = "SID";     //maybe(主表中子表PKID，在视图中也应有该字段)
    const string _TABLE_FIELD_PARENT_SOURCE = "Usource";   //maybe to edit(主要中需要编辑的字段，在视图中也应有该字段)
    //  从表数据字段
    const string _TABLE_FIELD_CHILD_PKID = "SID";     //maybe(子表的PKID，在视图中也应有该字段)
    const string _TABLE_FIELD_CHILD_NAME = "SName";   //maybe(子表的字段，在视图中也应有该字段)
    const string _TABLE_FIELD_CHILD_SEX = "SSex";     //maybet(子表的字段，在视图中也应有该字段)
    //  控件名
    const string _CTRL_HIDDENFIELD_PKID = "HiddenField1";   //隐藏表单PK_ID,与aspx页中控件名相对应
    const string _CTRL_LINKBUTTON_EDIT = "lkbtonEdit";      //进行编辑操作的LinkButton,与aspx页中控件名相对应
    const string _CTRL_LINKBUTTON_UPDATE = "lkbtnUpdate";   //进行更改操作的LinkButton,与aspx页中控件名相对应
    const string _CTRL_LABEL_SOURCE = "lblSource";      //maybet进行编辑操作前的Label,与aspx页中控件名相对应
    const string _CTRL_TEXTBOX_SOURCE = "txtSource";      //maybet进行更改操作的TextBox,与aspx页中控件名相对应
    //  业务处理对象名
    BMultiWindow bMultiWindow = new BMultiWindow();
    #endregion

    #region 事件
    /// <summary>
    /// 页面载入事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        //检查是否存在缓存数据
        if (Session[Constants.SESSION_TEMP_DATASET] == null)
        {
            //  maybe to edit(主表的SQL查询字符串)
            string strSQL = "select [UID],[SID],[Usource] from [demo_MultiLine_parent]";
            //  maybe to edit(视图的SQL查询字符串)
            string strSQLView = "SELECT [SID],[UID],[SName],[SSex],[Usource] FROM [vi_dome_multiLineManager]";

            DataSet ds = new DataSet();     //  存放数据
            DataSet dsView = new DataSet(); //  存放视图
            bMultiWindow.PageLoadMultiPage(ref GridView1,ref ds,ref dsView, strSQL, strSQLView);

            Session[Constants.SESSION_TEMP_DATASET] = ds;       //  将主表数据放到置到Session中
            Session[Constants.SESSION_UI_DATAVIEW] = dsView;    //  将视图中的数据放置到Session中

            //  设置已有的数据PK_ID
            Hashtable htSessionData = new Hashtable();
            foreach (DataRow dr in dsView.Tables[0].Rows)
            {
                /**------------------maybe start-----------------------------------
                 */
                //  1、更改参数对象类别
                DemoChildParams childParms = new DemoChildParams();
                childParms.PKID = dr[_TABLE_FIELD_CHILD_PKID] as string;

                //  2、设置相关参数对象属性
                childParms.Source = ((int)dr[_TABLE_FIELD_PARENT_SOURCE]).ToString();
                htSessionData[dr[_TABLE_FIELD_CHILD_PKID] as string] = childParms;
                /*---------------------maybe end----------------------------------*/
            }
            Session[Constants.SESSION_OLD_SELECTED] = htSessionData;

        }
    }

    /// <summary>
    /// GiideView 行删除操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lkbtnDel_Click(object sender, EventArgs e)
    {
        string strPkID = ((LinkButton)sender).CommandArgument;
        //  获取数据
        DataSet ds = (DataSet)Session[Constants.SESSION_TEMP_DATASET];
        DataSet dsView = (DataSet)Session[Constants.SESSION_UI_DATAVIEW];

        string childPkid = RowsEdit.RowDelete(ref ds,ref dsView, _TABLE_FIELD_PARENT_PKID, strPkID,_TABLE_FIELD_CHILD_PKID);
        //  重新放入SESSION
        Session[Constants.SESSION_TEMP_DATASET] = ds;
        Session[Constants.SESSION_UI_DATAVIEW] = dsView;
        //  更改Session中已选中的数据项
        Hashtable htOldDate = Session[Constants.SESSION_OLD_SELECTED] as Hashtable;
        htOldDate.Remove(childPkid);
        //  重新绑定数据
        GridView1.DataSource = dsView.Tables[0].DefaultView;
        GridView1.DataBind();
        dsView.Dispose();
        ds.Dispose();
    }

    /// <summary>
    /// 行编辑操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lkbtonEdit_Click1(object sender, EventArgs e)
    {
        /**------------------maybe start-----------------------------------
         * 说明：如有多个需要编辑字段，则需要在仿照现有的代码，在aspx页增加相应字段，在aspx.cs仿下列逻辑即可
         */
        string strIndex = ((LinkButton)sender).CommandArgument;

        LinkButton lkedit = GridView1.Rows[Convert.ToInt32(strIndex)].FindControl(_CTRL_LINKBUTTON_EDIT) as LinkButton;
        lkedit.Visible = false;
        LinkButton lkeupdate = GridView1.Rows[Convert.ToInt32(strIndex)].FindControl(_CTRL_LINKBUTTON_UPDATE) as LinkButton;
        lkeupdate.Visible = true;
        //  1、在这里设置Label不可见
        Label lbName = GridView1.Rows[Convert.ToInt32(strIndex)].FindControl(_CTRL_LABEL_SOURCE) as Label;
        lbName.Visible = false;
        //  2、在这里设置Text可见
        TextBox txName = GridView1.Rows[Convert.ToInt32(strIndex)].FindControl(_CTRL_TEXTBOX_SOURCE) as TextBox;
        txName.Visible = true;
        /*---------------------maybe end----------------------------------*/
    }

    /// <summary>
    /// 行更改操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lkbtnUpdate_Click(object sender, EventArgs e)
    {
        /**------------------maybe start-----------------------------------
         * 说明：如有多个需要编辑字段，则需要在仿照现有的代码，在aspx页增加相应字段，在aspx.cs仿下列逻辑即可
         */

        //  将超链接"更改"变为"编辑"
        string strIndex = ((LinkButton)sender).CommandArgument;
        int index = Convert.ToInt32(strIndex);
        LinkButton lkedit = GridView1.Rows[index].FindControl(_CTRL_LINKBUTTON_EDIT) as LinkButton;
        lkedit.Visible = true;
        LinkButton lkeupdate = GridView1.Rows[index].FindControl(_CTRL_LINKBUTTON_UPDATE) as LinkButton;
        lkeupdate.Visible = false;

        //  1、对编辑区转换
        Label lblName = GridView1.Rows[index].FindControl(_CTRL_LABEL_SOURCE) as Label;
        lblName.Visible = true;
        TextBox txtName = GridView1.Rows[index].FindControl(_CTRL_TEXTBOX_SOURCE) as TextBox;
        txtName.Visible = false;

        //  对缓存数据集更新
        HiddenField hfdPkid = GridView1.Rows[index].FindControl(_CTRL_HIDDENFIELD_PKID) as HiddenField;
        string rowPkid = hfdPkid.Value;
        DataTable ds = ((DataSet)Session[Constants.SESSION_TEMP_DATASET]).Tables[0];
        DataTable dsView = ((DataSet)Session[Constants.SESSION_UI_DATAVIEW]).Tables[0];
        int i = -1;
        string strSource = txtName.Text;
        string childPkid = "";
        foreach (DataRow dr in dsView.Rows)
        {
            if (dr.RowState != DataRowState.Deleted)
            {
                if (dr[_TABLE_FIELD_PARENT_PKID].ToString() == rowPkid)
                {
                    i = dsView.Rows.IndexOf(dr);
                    //  2、获取文本框的值
                    dsView.Rows[i][_TABLE_FIELD_PARENT_SOURCE] = txtName.Text;
                    //  3、更新文本框的值到数据集中
                    ds.Rows[i][_TABLE_FIELD_PARENT_SOURCE] = strSource;
                    childPkid = ds.Rows[i][_TABLE_FIELD_CHILD_PKID] as string;
                    break;
                }
            }
        }
        //  4、更新Session中数据
        Hashtable htSessionDate = Session[Constants.SESSION_OLD_SELECTED] as Hashtable;
        (htSessionDate[childPkid] as DemoChildParams).Source = strSource;
        //  重新绑定数据
        GridView1.DataSource = dsView.DefaultView;
        GridView1.DataBind();
        ds.Dispose();
        /*-------------------------maybe end--------------------------------*/
    }

    /// <summary>
    /// 添加新数据操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnHiddenForUpdate_ServerClick(object sender, EventArgs e)
    {
        /**------------------maybe start-----------------------------------
         * 说明：主表的SQL查询字符串,使用时需修改对应字段即可
         */
        if (Session[Constants.SESSION_NEW_SELECTED] != null && (Session[Constants.SESSION_NEW_SELECTED] as Hashtable).Count>0)
        {
            Hashtable htNewDate = (Session[Constants.SESSION_NEW_SELECTED] as Hashtable);

            string sqlwhere = "";
            foreach (DictionaryEntry de  in htNewDate)
            {
                sqlwhere += sqlwhere + de.Key as string + ",";
            }
            //  1、设置SQL语句
            string strSQL = @"select [SID],[SName],[SSex] from [demo_MultiLine_child] where [SID] IN ('"
                + sqlwhere.Substring(0, sqlwhere.Length - 1).Replace(",", @"','") + @"')";

            DataSet dsDate = RowsEdit.GetDataSet(strSQL);
            DataTable dsSave = ((DataSet)Session[Constants.SESSION_TEMP_DATASET]).Tables[0];
            DataTable dsView = ((DataSet)Session[Constants.SESSION_UI_DATAVIEW]).Tables[0];
            foreach (DataRow dr in dsDate.Tables[0].Rows)
            {
                DataRow drSave = dsSave.NewRow();
                DataRow drView = dsView.NewRow();
                //获取UUID
                string strPkid = UUIDGenerator.GetUUID();  //  
                //  2、设置选中的数据
                drSave[_TABLE_FIELD_PARENT_PKID] = strPkid;
                drSave[_TABLE_FIELD_PARENT_SID] = dr[_TABLE_FIELD_CHILD_PKID];
                string childPkid = dr[_TABLE_FIELD_CHILD_PKID] as string;
                drSave[_TABLE_FIELD_PARENT_SOURCE] = (htNewDate[childPkid] as DemoChildParams).Source;
                dsSave.Rows.Add(drSave);
                //  3、设置选中的数据
                drView[_TABLE_FIELD_PARENT_PKID] = strPkid;
                drView[_TABLE_FIELD_PARENT_SID] = dr[_TABLE_FIELD_CHILD_PKID];
                drView[_TABLE_FIELD_PARENT_SOURCE] = (htNewDate[childPkid] as DemoChildParams).Source;
                drView[_TABLE_FIELD_CHILD_NAME] = dr[_TABLE_FIELD_CHILD_NAME];
                drView[_TABLE_FIELD_CHILD_SEX] = dr[_TABLE_FIELD_CHILD_SEX];
                dsView.Rows.Add(drView);
            }
            GridView1.DataSource = dsView.DefaultView;
            GridView1.DataBind();
            Session.Remove(Constants.SESSION_NEW_SELECTED);
        }
        /*-------------------------maybe end--------------------------------*/

    }

    /// <summary>
    /// 数据提交
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DataSet ds = (DataSet)Session[Constants.SESSION_TEMP_DATASET];
        int i = 0;  //  记录影响行数

        /**------------------maybe start-----------------------------------
         * 说明：主表的SQL查询字符串,使用时需修改对应字段即可
         */
        //  1、指定SQL查询语句
        string strSQL = "select [UID],[SID],[Usource] from [demo_MultiLine_parent]";
 
        i = RowsEdit.DataSubmit(ds, strSQL);
        Session.Remove(Constants.SESSION_NEW_SELECTED);
        Session.Remove(Constants.SESSION_OLD_SELECTED);
        Session.Remove(Constants.SESSION_TEMP_DATASET);
        Session.Remove(Constants.SESSION_UI_DATAVIEW);

        if (i > 0)
        {
            //  2、指定跳转页,如要需要，可相应增加跳转逻辑
            Response.Redirect("Result.html");
        }
        /*-------------------------maybe end--------------------------------*/
    }
    #endregion
}
